#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextEdit>
#include <QActionGroup>
#include "myaction.h"
#include <QToolButton>
#include <QSpinBox>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QMenu* editMenu = ui->menubar->addMenu(tr("编辑(&E)"));
    QAction * action_Open = editMenu->addAction(tr("打开文件(&O)"));
    action_Open->setShortcut(QKeySequence("Ctrl+o"));
    connect(action_Open,&QAction::triggered, this, &MainWindow::on_action_open_triggered);

    auto group = new QActionGroup(this);
    auto action_L = group->addAction(tr("左居中"));
    auto action_R = group->addAction(tr("右居中"));
    auto action_C = group->addAction(tr("居中"));

    editMenu->addSeparator();
    action_L->setCheckable(true);
    action_R->setCheckable(true);
    action_C->setCheckable(true);
    editMenu->addAction(action_R);
    editMenu->addAction(action_L);
    editMenu->addAction(action_C);

    //添加自定义的一个文本输入
    auto myaction = new MyAction(this);
    editMenu->addAction(myaction);
    connect(myaction,&MyAction::getText,this,&MainWindow::getText);

    //工具栏添加元素
    QToolButton * toolBtn = new QToolButton(this);
    toolBtn->setText(tr("颜色"));
    QMenu* colorMenu = new QMenu(this);
    colorMenu->addAction(tr("红色"));
    colorMenu->addAction(tr("绿色"));
    toolBtn->setMenu(colorMenu);
    toolBtn->setPopupMode(QToolButton::MenuButtonPopup);
    ui->toolBar->addWidget(toolBtn);
    QSpinBox* spinBox = new QSpinBox(this);
    ui->toolBar->addSeparator();
    ui->toolBar->addWidget(spinBox);

    //显示临时消息
    ui->statusbar->showMessage(tr("欢迎使用多文档编辑器"),2000);
    //创建标签
    QLabel* permanent = new QLabel(this);
    permanent->setFrameStyle(QFrame::Box | QFrame::Sunken);
    permanent->setText("zihao");
    ui->statusbar->addPermanentWidget(permanent);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionxianshidock_D_triggered()
{
    ui->dockWidget->show();
}


void MainWindow::on_actionxinjian_N_triggered()
{
    auto child = new QTextEdit(this);
    ui->mdiArea->addSubWindow(child);
    child->setWindowTitle(tr("请输入文本！"));
    child->show();
}

void MainWindow::getText(const QString &text)
{
    ui->textEdit->setText(text);
}

void MainWindow::on_action_open_triggered()
{
    qDebug() << "打开文件..." << Qt::endl;
}
